import { BasicColumn } from "/@/components/Table"
import { FormSchema } from "/@/components/Table"
import { rules } from "/@/utils/helper/validator"
import { render } from "/@/utils/common/renderUtils"
import { UploadBizType } from "/@/enums/uploadEnum"
import { DescItem } from "/@/components/Description/index"
import { listMemberLevel } from "/@/api/member/level.api"

export const ACCOUNT_TYPE = {
  POINT: "1",
  EXPERIENCE: "2",
}

export const columns: BasicColumn[] = [
  {
    title: "头像",
    dataIndex: "avatar",
    width: 80,
    customRender: (record) => {
      return render.renderAvatar(record.avatar)
    },
  },
  {
    title: "用户账号",
    dataIndex: "userName",
    width: 100,
  },
  {
    title: "用户姓名",
    dataIndex: "realName",
    width: 100,
  },
  {
    title: "手机号",
    dataIndex: "phone",
    width: 100,
  },
  {
    title: "会员等级",
    dataIndex: "levelName",
    width: 100,
  },
  {
    title: "注册终端",
    dataIndex: "registerTerminal",
    customRender: ({ text }) => {
      const color = "green"
      return render.renderTag(render.renderDict(text, "terminal"), color)
    },
    width: 100,
  },
  {
    title: "状态",
    dataIndex: "status",
    customRender: ({ text }) => {
      let color = "green"
      if (text === "2") {
        color = "red"
      }
      return render.renderTag(render.renderDict(text, "user_status"), color)
    },
    width: 60,
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    label: "账号",
    field: "userName",
    component: "Input",
  },
  {
    label: "注册终端",
    field: "registerTerminal",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "terminal",
      placeholder: "全部",
      stringToNumber: true,
    },
  },
  {
    label: "真实名称",
    field: "realName",
    component: "Input",
  },
  {
    label: "手机号码",
    field: "phone",
    component: "Input",
  },
  {
    label: "用户状态",
    field: "status",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "user_status",
      placeholder: "请选择状态",
      stringToNumber: true,
    },
  },
]

export const formSchema: FormSchema[] = [
  {
    label: "",
    field: "id",
    component: "Input",
    show: false,
  },
  {
    label: "用户账号",
    field: "userName",
    required: true,
    component: "Input",
    helpMessage: "用于登录，添加成功后不可编辑，请认真填写",
    dynamicDisabled: ({ values }) => {
      return !!values.id
    },
    // dynamicRules: ({model,schema}) =>rules.duplicateCheckRule("sys_user", "userName",model,schema,true),
  },
  {
    label: "用户姓名",
    field: "realName",
    component: "Input",
  },
  {
    label: "用户昵称",
    field: "nickName",
    component: "Input",
  },
  {
    label: "会员等级",
    field: "levelId",
    component: "ApiSelect",
    componentProps: {
      api: listMemberLevel,
      optionFilterProp: "label",
      showSearch: true,
      labelField: "name",
      valueField: "id",
    },
  },

  {
    label: "登录密码",
    field: "password",
    component: "StrengthMeter",
    componentProps: {
      placeholder: "请输入",
      autocomplete: "false",
    },
    rules: [
      {
        required: true,
        message: "请输入登录密码",
      },
    ],
  },
  {
    label: "确认密码",
    field: "confirmPassword",
    component: "InputPassword",
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
  {
    label: "头像",
    field: "avatar",
    component: "MyImageUpload",
    componentProps: {
      fileMax: 1,
      bizType: UploadBizType.GENERAL_IMAGE,
    },
  },
  {
    label: "生日",
    field: "birthday",
    component: "DatePicker",
  },
  {
    label: "性别",
    field: "sex",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "sys_user_sex",
      placeholder: "请选择",
      // stringToNumber: true
    },
  },
  {
    label: "邮箱",
    field: "email",
    component: "Input",
    rules: rules.rule("email", false),
    dynamicRules: ({ model, schema }) => {
      if (!model.id || (model.id && model.email?.indexOf("*") !== -1)) {
        return [{ required: false }]
      }
      return rules.rule("email", false)
    },
  },
  {
    label: "手机号码",
    field: "phone",
    component: "Input",
    dynamicRules: ({ model, schema }) => {
      if (!model.phone || (model.id && model.phone?.indexOf("*") !== -1)) {
        return [{ required: false }]
      }
      return [{ pattern: /^1[3|4|5|7|8|9][0-9]\d{8}$/, message: "手机号码格式有误" }]
    },
  },
]

/**
 * 调整积分
 */
export const adjustPointFormSchema: FormSchema[] = [
  {
    label: "",
    field: "id",
    component: "Input",
    show: false,
  },
  {
    label: "当前积分",
    field: "point",
    component: "Input",
    render: ({ model, field }) => {
      return model[field]
    },
  },
  {
    label: "调整积分",
    field: "updatePoint",
    component: "InputNumber",
  },
  {
    field: "remark",
    label: "备注",
    component: "InputTextArea",
    componentProps: {
      autoSize: {
        minRows: 4,
        maxRows: 8,
      },
    },
  },
]

export const formPasswordSchema: FormSchema[] = [
  {
    label: "用户账号",
    field: "userName",
    component: "Input",
    componentProps: { readonly: true },
  },
  {
    label: "",
    field: "id",
    show: false,
    component: "Input",
  },
  {
    label: "登录密码",
    field: "password",
    component: "StrengthMeter",
    componentProps: {
      placeholder: "请输入登录密码",
    },
    rules: [
      {
        required: true,
        message: "请输入登录密码",
      },
    ],
  },
  {
    label: "确认密码",
    field: "confirmPassword",
    component: "InputPassword",
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
  },
]

/**
 * 搜索用户
 */
export const searchUserFormSchema: FormSchema[] = [
  {
    field: "userName",
    label: "用户账号",
    component: "Input",
    colProps: { span: 12 },
  },
  {
    field: "test",
    label: "",
    component: "Input",
    slot: "addUser",
    colProps: { span: 12 },
  },
]

/**
 * 用户基本信息
 */
export const baseInfoDescSchema: DescItem[] = [
  {
    field: "userName",
    label: "用户名",
    span: 2,
  },
  {
    field: "nickName",
    label: "昵称",
    span: 2,
  },
  {
    field: "phone",
    label: "联系电话",
    span: 2,
  },
  {
    field: "email",
    label: "邮箱",
    span: 2,
  },
  {
    field: "registerIp",
    label: "注册IP",
    span: 2,
  },
  {
    field: "registerTerminal",
    label: "注册终端",
    span: 2,
    render: (curVal, data) => {
      return render.renderDict(curVal, "terminal")
    },
  },
  {
    field: "sex",
    label: "性别",
    span: 2,
    render: (curVal, data) => {
      return render.renderDict(curVal, "sys_user_sex")
    },
  },

  {
    field: "birthday",
    label: "生日",
    span: 2,
  },
  {
    field: "lastLoginDate",
    label: "最后登录时间",
    span: 2,
  },
  {
    field: "levelName",
    label: "会员等级",
    span: 4,
  },
  {
    field: "lastLoginIp",
    label: "最后登录IP",
    span: 4,
  },

  {
    field: "address",
    label: "地址",
    span: 4,
    render: (curVal, data) => {
      return curVal && curVal !== "" ? `${data.fullAddress}-${curVal}` : data.fullAddress
    },
  },
]

/**
 * 用户权益信息
 */

export const equityInfoDescSchema: DescItem[] = [
  {
    field: "point",
    label: "积分",
    span: 2,
  },
  {
    field: "experience",
    label: "成长值",
    span: 2,
  },
]

/**
 * 账户明细
 */
export const accountDetailColumn: BasicColumn[] = [
  {
    title: "账户类型",
    dataIndex: "accountTypeName",
    width: 100,
  },
  {
    title: "数据金额",
    dataIndex: "accountData",
    customRender: ({ text }) => {
      return text > 0 ? `+${text}` : `${text}`
    },
    width: 100,
  },
  {
    title: "发生方式",
    dataIndex: "fromTypeName",
    width: 100,
  },
  {
    title: "发生时间",
    dataIndex: "createdAt",
    width: 160,
  },
  {
    title: "备注",
    dataIndex: "remark",
    width: 200,
  },

  // {
  //   title: '注册终端',
  //   dataIndex: 'registerTerminal',
  //   customRender: ({text}) => {
  //     let color = 'green'
  //     return render.renderTag(render.renderDict(text, 'terminal'), color)
  //   },
  //   width: 100,
  // }
]

const accountDetailBaseFormSchema: FormSchema[] = [
  {
    field: "fieldTime",
    label: "开始时间",
    component: "RangePicker",
    colProps: { span: 24 },
    componentProps: {
      valueType: "Date",
    },
  },
]

/**
 * 积分搜索表单
 */
export const pointAccountSearchFormSchema: FormSchema[] = [
  {
    label: "来源类型",
    field: "fromType",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "member_account_from",
      filter: (e) => ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(e),
      placeholder: "全部",
    },
  },
  ...accountDetailBaseFormSchema,
]

/**
 * 成长值搜索表单
 */
export const experienceAccountSearchFormSchema: FormSchema[] = [
  {
    label: "来源类型",
    field: "fromType",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "member_account_from",
      filter: (e) => ["3", "4", "5", "6", "7", "8"].includes(e),
      placeholder: "全部",
    },
  },
  ...accountDetailBaseFormSchema,
]
